/*
    SUSAN® - Sole of Unix Save ANything

   
   Copyright (C) 2014-2019 Hechzh Property

   
*/
/* originally was Kern Sibbald, July MMIII */
/*
 * extracted the TEST_PROGRAM functionality from the files in ..
 * and adapted for gtest
 *
 * Philipp Storz, November 2017
 */
#if defined(HAVE_MINGW)
#include "include/susan.h"
#include "gtest/gtest.h"
#else
#include "gtest/gtest.h"
#include "include/susan.h"
#endif

#include "lib/htable.h"

struct HTABLEJCR {
#ifndef TEST_NON_CHAR
  char* key;
#else
  uint32_t key;
#endif
  hlink link;
};

#ifndef TEST_SMALL_HTABLE
#define NITEMS 5000000
#else
#define NITEMS 5000
#endif
TEST(htable, htable)
{
  char mkey[30];
  htable* jcrtbl;
  HTABLEJCR *save_jcr = NULL, *item;
  HTABLEJCR* jcr = NULL;
  int count = 0;

  jcrtbl = (htable*)malloc(sizeof(htable));

#ifndef TEST_SMALL_HTABLE
  jcrtbl->init(jcr, &jcr->link, NITEMS);
#else
  jcrtbl->init(jcr, &jcr->link, NITEMS, 128);
#endif
  for (int i = 0; i < NITEMS; i++) {
#ifndef TEST_NON_CHAR
    int len;
    len = sprintf(mkey, "%d", i) + 1;

    jcr = (HTABLEJCR*)jcrtbl->hash_malloc(sizeof(HTABLEJCR));
    jcr->key = (char*)jcrtbl->hash_malloc(len);
    memcpy(jcr->key, mkey, len);
#else
    jcr = (HTABLEJCR*)jcrtbl->hash_malloc(sizeof(HTABLEJCR));
    jcr->key = i;
#endif

    jcrtbl->insert(jcr->key, jcr);
    if (i == 10) { save_jcr = jcr; }
  }
  EXPECT_TRUE(item = (HTABLEJCR*)jcrtbl->lookup(save_jcr->key));
  foreach_htable (jcr, jcrtbl) {
    count++;
  }

  jcrtbl->destroy();
  EXPECT_EQ(count, NITEMS);
  free(jcrtbl);
}

struct RbListJobControlRecord {
  char* buf;
};

